import {defineStore} from "pinia";
import {debounce} from "lodash";
import FlowNodeDataApi from "@/api/flow/FlowNodeDataApi";

export const useNodeConfigFormStore = defineStore({
    'useNodeConfigFormStore',
    state: () => ({
        NodeConfigData: {} // 节点的配置参数
    }),
    actions: {
        CommitNodeConfig(NodeConfig: any) {
            this.NodeConfigData = NodeConfig
        },

        // 防抖保存（核心：任何组件修改数据都会触发）
        UpdateNodeConfig: debounce(async function () {
            try {
                // 模拟API保存请求
                console.log('自动保存全量数据：', this.formData);
                await new Promise(resolve => setTimeout(resolve, 500));
                // new FlowNodeDataApi().Install(this.currentClickNodeMixin.id, params, NodeName, false, NodeCommonInfo.NodeTimeOut)
                this.saveSuccess = true;
                // 3秒后隐藏成功提示
                setTimeout(() => this.saveSuccess = false, 3000);
            } catch (error) {
                console.error('保存失败：', error);
            } finally {
                this.saving = false;
            }
        }, 500) // 500ms防抖，避免频繁提交
    }
})
